<template>
    <div class="my-exam-page">
      <div class="exam-list-block">
        <div class="exam-list-header">
          <span class="ex-title">我的竞赛</span>
          <span class="exam-list-back" @click="goBack">返回</span>
        </div>
        <div class="exam-list-content" v-for="(exam, index) in myExamList" :key="index">
          <img src="@/assets/ide/jingsai.png" class="image" />
          <div class="exam-content">
            <div class="title">
              {{ exam.title }}
            </div>
            <div class="date"><span>比赛时间:</span> {{ exam.startTime }} ~ {{ exam.endTime }}</div>
          </div>
          <div>
            <div class="exam-end-lable-list" v-if="isHistoryExam(exam)">
              <span class="exam-end-lable">已完赛</span>
              <el-button class="exam-rank-lable" type="text" @click="togglePopover(exam.examId)">查看排名</el-button>
              <el-button class="exam-rank-lable" type="text" @click="goHistoryExam(exam)">竞赛练习</el-button>
            </div>
            <div class="exam-status-lable exam-end-lable-list" v-else-if="isNotStart(exam)">未开赛</div>
            <div class="exam-end-lable-list" v-else>
              <el-button class="exam-rank-lable" type="text" plain @click="goExam(exam)">开始答题</el-button>
            </div>
          </div>
        </div>
        <div class="my-exam-pagination">
          <!-- 增加分页展示器 -->
          <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="total"
            v-model:current-page="params.pageNum" v-model:page-size="params.pageSize" :page-sizes="[5, 10, 15, 20]"
            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { reactive, ref } from 'vue'
  import {
    getMyExamListService,
  } from "@/apis/exam"
  import router from '@/router'
  
  const myExamList = ref([]) //消息列表
  
  const total = ref(0)
  const params = reactive({
    pageNum: 1,
    pageSize: 10,
  })
  
  // 分页
  function handleSizeChange(newSize) {
    params.pageNum = 1
    getMyExamList()
  }
  
  function handleCurrentChange(newPage) {
    getMyExamList()
  }
  
  //消息列表
  async function getMyExamList() {
    const ref = await getMyExamListService(params.value)
    myExamList.value = ref.rows
    total.value = ref.total
  }
  getMyExamList()
  
  function isHistoryExam(exam) {
    const now = new Date();
    return new Date(exam.endTime) < now;
  }
  
  const isNotStart = (exam) => {
    const now = new Date(); //当前时间
    return new Date(exam.startTime) > now;
  }
  
  const goBack = () => {
    router.go(-1)
  }
  
  </script>
  
  <style lang="scss">
  .my-exam-page {
    background-color: #f8fafc;
    position: relative;
    overflow: hidden;
    min-height: calc(100vh - 64px);
    padding: 24px;
    animation: fadeIn 0.5s ease-out;
    display: flex;
    justify-content: center;

    .exam-list-block {
      width: 100%;
      max-width: 1200px;
      background: #fff;
      border-radius: 16px;
      box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
      padding: 24px;
      transition: all 0.3s ease;

      &:hover {
        box-shadow: 0 8px 16px -4px rgba(0, 0, 0, 0.1);
      }

      .exam-list-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 16px 24px;
        border-bottom: 1px solid #e2e8f0;
        margin-bottom: 24px;

        .ex-title {
          font-family: PingFangSC, PingFang SC;
          font-weight: 600;
          font-size: 24px;
          color: #1e293b;
          position: relative;
          padding-left: 16px;

          &::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 20px;
            background: linear-gradient(135deg, #32C5FF 0%, #3EC8FF 100%);
            border-radius: 2px;
          }
        }

        .exam-list-back {
          cursor: pointer;
          color: #64748b;
          font-size: 14px;
          padding: 8px 16px;
          border-radius: 8px;
          transition: all 0.3s ease;

          &:hover {
            color: #32C5FF;
            background: #f1f5f9;
            transform: translateX(-4px);
          }
        }
      }

      .exam-list-content {
        background: #fff;
        border-radius: 12px;
        margin-bottom: 16px;
        padding: 20px;
        display: flex;
        align-items: center;
        transition: all 0.3s ease;
        animation: slideIn 0.5s ease-out;
        position: relative;
        overflow: hidden;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 8px 16px -4px rgba(0, 0, 0, 0.1);
        }

        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          height: 100%;
          width: 4px;
          background: linear-gradient(135deg, #32C5FF 0%, #3EC8FF 100%);
          border-radius: 0 2px 2px 0;
        }

        .image {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          margin-right: 20px;
          transition: all 0.3s ease;

          &:hover {
            transform: scale(1.1);
          }
        }

        .exam-content {
          flex: 1;
          margin-right: 20px;

          .title {
            font-family: PingFangSC, PingFang SC;
            font-weight: 600;
            font-size: 16px;
            color: #1e293b;
            line-height: 1.4;
            margin-bottom: 8px;
          }

          .date {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 14px;
            color: #64748b;
            line-height: 1.5;

            span {
              color: #94a3b8;
              margin-right: 8px;
            }
          }
        }

        .exam-end-lable-list {
          display: flex;
          align-items: center;
          gap: 12px;
          padding-right: 20px;

          .exam-end-lable {
            font-family: PingFangSC, PingFang SC;
            font-weight: 500;
            font-size: 14px;
            color: #94a3b8;
            padding: 6px 12px;
            background: #f1f5f9;
            border-radius: 6px;
          }

          .exam-rank-lable {
            font-family: PingFangSC, PingFang SC;
            font-weight: 500;
            font-size: 14px;
            color: #32C5FF;
            padding: 6px 12px;
            background: #f1f5f9;
            border-radius: 6px;
            transition: all 0.3s ease;

            &:hover {
              background: #32C5FF;
              color: #fff;
              transform: translateY(-2px);
              box-shadow: 0 4px 6px -1px rgba(50, 197, 255, 0.2);
            }
          }
        }

        .exam-status-lable {
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 14px;
          color: #94a3b8;
          padding: 6px 12px;
          background: #f1f5f9;
          border-radius: 6px;
        }
      }

      .my-exam-pagination {
        margin-top: 24px;
        display: flex;
        justify-content: center;
      }
    }
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  @keyframes slideIn {
    from {
      transform: translateX(-20px);
      opacity: 0;
    }
    to {
      transform: translateX(0);
      opacity: 1;
    }
  }
  </style>